<?php

namespace App\Admin\Controllers\order;

use App\Admin\Extensions\Filter\TimestampBetween;
use App\Models\Order\Info;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class VipController extends Controller
{
    use HasResourceActions;
    public $header = '老师订单列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        /*$result = Info::with('user')->orderBy('id', 'desc')->paginate();

        dd($result->toArray());*/

        return Admin::grid(Info::class, function (Grid $grid) {

            //$grid->model()->with('user')->where('type', Info::TYPE_VIP)->orderBy('id','desc');
            $grid->model()->leftJoin('jxt_xp_user as b', 'jxt_order_info.user_id', 'b.id')
                ->where('jxt_order_info.type', Info::TYPE_VIP)
                ->orderBy('jxt_order_info.id','desc')
                ->selectRaw('jxt_order_info.*,b.id user_id,b.username,b.telephone');

            //dd($grid->user()->id);

            $grid->id('订单ID')->sortable();
            $grid->user_id('用户id');
            $grid->username('用户名');
            $grid->telephone('手机号');
            $grid->order_sn('订单编号');
            $grid->order_amount('订单金额');
            $grid->create_time('创建时间')->display(function ($create_time){
                if (!$create_time) return '';
                return date('Y-m-d H:i:s', $create_time);
            });
            $grid->pay_status('订单状态')->using(Info::$pay_status);
            $grid->pay_time('支付时间')->display(function ($pay_time){
                if (!$pay_time) return '';
                return date('Y-m-d H:i:s', $pay_time);
            });
            $grid->mobile_type('手机类型')->using(Info::$mobile_type);

            $grid->disableActions();
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter){

                $filter->disableIdFilter();
                $filter->like('order_sn', '订单编号');
                $filter->equal('pay_status', '订单状态')->select(Info::$pay_status);
                $filter->use(new TimestampBetween('pay_time', '支付时间'))->datetime();
                $filter->equal('mobile_type', '手机类型')->select(Info::$mobile_type);

            });

        });
    }

}
